#include <errno.h>
#include "base/notification.h"
#include "base/logging.h"

Notification::Notification()
    : mutex_(PTHREAD_MUTEX_INITIALIZER),
      cond_(PTHREAD_COND_INITIALIZER),
      notified_(false) {
}
 
void Notification::WaitForNotification() {
  CHECK_EQ(pthread_mutex_lock(&mutex_), 0);
  if (!notified_) {
    pthread_cond_wait(&cond_, &mutex_);
  }
  CHECK_EQ(pthread_mutex_unlock(&mutex_), 0);
}

void Notification::Notify() {
  CHECK_EQ(pthread_mutex_lock(&mutex_), 0);
  pthread_cond_signal(&cond_);
  notified_ = true;
  CHECK_EQ(pthread_mutex_unlock(&mutex_), 0);
}
